<template>
  <div>
    <!-- 搜索中心 导航栏 -->
    <van-nav-bar title="搜索中心" left-arrow @click-left="$router.back()" />
    <!-- 搜索框 -->
    <van-search
      v-model="key"
      show-action
      @input="touchInfo"
      placeholder="请输入搜索关键词"
      @search="onSearch"
    >
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search>
    <!-- 历史记录区域 -->
    <van-cell-group v-if="!key">
      <van-cell title="历史记录">
        <template #right-icon>
          <van-icon name="delete-o" class="delete-icon" @click="delAll" />
        </template>
      </van-cell>
      <van-cell v-for="(item, index) in historyList" :key="index">
        <template>
          <div class="historyList">
            <div @click="assignment(item)" class="name">{{ item }}</div>
            <van-icon name="cross" @click="delHistory(index)" />
          </div>
        </template>
      </van-cell>
    </van-cell-group>
    <!-- 关键词区域 -->
    <van-cell-group v-else>
      <van-cell
        icon="search"
        v-for="(item, index) in suggestColor"
        :key="index"
        @click="$router.push(`/search/result?key=${suggestList[index]}`)"
      >
        <div v-html="item"></div>
      </van-cell>
    </van-cell-group>
  </div>
</template>

<script>
import { suggestionAPI } from '@/api/search'
export default {
  data() {
    return {
      key: '',
      suggestList: [],
      historyList: JSON.parse(localStorage.getItem('historyInfo')) || []
    }
  },
  methods: {
    delAll() {
      this.historyList = []
      localStorage.removeItem('historyInfo')
    },
    delHistory(index) {
      this.historyList.splice(index, 1)
      localStorage.setItem('historyInfo', JSON.stringify(this.historyList))
    },
    assignment(item) {
      this.$router.push({
        name: 'result',
        query: {
          key: item
        }
      })
    },
    async touchInfo() {
      if (this.key == '') return
      const res = await suggestionAPI({
        q: this.key
      })
      if (res.data.options[0]) {
        this.suggestList = res.data.options
      } else {
        this.suggestList = []
      }
    },
    onSearch() {
      if (this.key == '') return this.$toast('输入内容不能为空')
      this.$router.push({
        name: 'result',
        query: {
          key: this.key
        }
      })
      const idx = this.suggestList.indexOf(v => v == this.key)
      if (!idx == -1) return
      this.historyList.push(this.key)
      localStorage.setItem('historyInfo', JSON.stringify(this.historyList))
    }
  },
  computed: {
    suggestColor() {
      return this.suggestList.map(v =>
        v.replace(this.key, `<span style="color:red;">${this.key}</span>`)
      )
    }
  }
}
</script>

<style lang="less" scoped>
.historyList {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .name {
    padding: 3px 10px;
    line-height: 20px;
    height: 20px;
    border-radius: 12px;
    background-color: rgba(188, 188, 188, 0.3);
    font-size: 14px;
  }
}
.delete-icon {
  font-size: 16px;
  line-height: inherit;
}
.van-cell__title {
  span {
    font-size: 18px;
    font-weight: 700;
  }
}
</style>
